﻿package com.boyaa.shells.tool
{
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	/**
	 * 加载图片
	 * @author fly
	 */ 
	 
	public class ImgLoaderIM
	{
		private var _conteiner:MovieClip = null;
		private var _url:String = "";
		private var _ani:MovieClip = null;
		private var _img:DisplayObject = null;
		private var _loader:Loader = null;
		private var _conteinerWidth:Number = 0;
		private var _conteinerHeight:Number = 0;
		
		public function ImgLoaderIM(conteiner:MovieClip,ani:MovieClip) 
		{
			_conteiner = conteiner;
			_conteinerWidth = _conteiner.width;
			_conteinerHeight = _conteiner.height;
			_img = _conteiner.getChildAt(0);
			_ani = ani;	
		}
		
		// 重置默认头像
		public function resetImg():void
		{
			removeLoader();
			while (_conteiner.numChildren) 
			{
				//_conteiner.removeChildAt(_conteiner.numChildren-1);		// 移除旧对象
				_conteiner.removeChildAt(0);		// 移除旧对象
			}
			_ani.stop();
			_ani.visible = false;
			_conteiner.addChild(_img);
			_conteiner.visible = true;
		}
		
		// 读取头像
		public function loadImg(url:String):void
		{
			_url = url;
		
			//var obj:DisplayObject = HeadContainer.seekData(_url);
			//if (obj != null)
			//{
				//_ani.visible = false ;
				//_ani.stop();
				//obj.width = _conteinerWidth;
				//obj.height = _conteinerHeight;
				//_conteiner.addChild(obj);
				//return ;
			//}
			if (_url != "") 
			{
				removeLoader();
				resetImg();
				_ani.visible = true ;
				_ani.play();
				_conteiner.visible = false;
				_loader = new Loader();
				_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadImgComplete);
				_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				
				_loader.load(new URLRequest(_url));
			}
			else
			{
				//_ani.visible = false ;
				//_ani.stop();
				resetImg();
			}
		}
		
		
		private function ioErrorHandler(event:IOErrorEvent):void 
		{
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadImgComplete);
           try{
				
			}
			catch (e:Error)
			{
				
			}
        }
		
		// 移除loader
		private function removeLoader():void
		{
			if (_loader != null) 
			{
				_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadImgComplete);
				_loader = null;
			}
		}
		
		// 读取头像完成
		private function onLoadImgComplete(e:Event):void 
		{
			e.currentTarget.removeEventListener(Event.COMPLETE, onLoadImgComplete);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			_ani.visible = false ;
			_ani.stop();
			_conteiner.visible = true;
			while (_conteiner.numChildren) 
			{
				_conteiner.removeChildAt(_conteiner.numChildren-1);		// 移除旧对象
			}
			
			var sprite:Sprite = new Sprite();			// 创建头像容器
			sprite.addChild(_loader);					
			
			sprite.width = _conteinerWidth;
			sprite.height = _conteinerHeight;
			_conteiner.addChild(sprite);		// 加入容器
			//HeadContainer.addData(_url, sprite);
			_loader = null; // 设为空
		}
	}
}